
import bitcoin
valid_private_key = False

while not valid_private_key:
    private_key = bitcoin.random_key()
    decode_private_key = bitcoin.decode_privkey(private_key)
    valid_private_key = 9 < decode_private_key < bitcoin.N

print("private key(hex) is:", private_key)
print("private key(decimal) is:",decode_private_key)


# 转化为 wif格式

wifEncodePrivateKey = bitcoin.encode_privkey(decode_private_key, 'wif')

print("private key(wif) is:", wifEncodePrivateKey)


compressedPrivateKey = private_key + '01'
print("private key(compressed hex) is:", compressedPrivateKey)

wifCompressedPrivateKey = bitcoin.encode_privkey(bitcoin.decode_privkey(compressedPrivateKey, 'hex'), 'wif')
print("private key(wif compressed) is:", wifCompressedPrivateKey)


public_key_x_y = bitcoin.privkey_to_pubkey(decode_private_key) # 用decode 的能够生成 x,y格式的 public key
print("public key:", public_key_x_y)
publicKeyHex = bitcoin.encode_pubkey(public_key_x_y, 'hex')
print("public key(hex):", publicKeyHex)


(public_key_x, public_key_y) = public_key_x_y

if (public_key_y % 2 ) == 0:
    compressedPrefix = '02'
else:
    compressedPrefix = '03'

hexCompressedPublic = compressedPrefix + bitcoin.encode(public_key_x, 16)
print("compressed public key(hex) is:", hexCompressedPublic)

print("bitcoin address (b58 check) is:", bitcoin.pubkey_to_address(public_key_x_y))
print("bitcoin compressed address(b58 check) is:", bitcoin.pubkey_to_address(hexCompressedPublic))